package com.metronoteapp.dialog;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import com.metronoteapp.MainActivity;
import com.metronoteapp.R;
import com.metronoteapp.database.GroupNoteDao;
import com.metronoteapp.model.GroupModel;

public class AddGroupDialog extends Dialog implements
		android.view.View.OnClickListener {
	private Context mContext;
	private Dialog dialog;
	private TextView txtGroupName;
	private Button btnOk;
	private String mColorGroup;
	private Button btnCancel;
	private Button btnColor1;
	private Button btnColor2;
	private Button btnColor3;
	private Button btnColor4;
	private Button btnColor5;
	private GroupNoteDao mObjGroupDao;
	private GroupModel mObjGroupModel;

	public AddGroupDialog(Context context) {
		super(context);
		mContext = context;
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btnOk:
			if(txtGroupName.getText().toString().trim().equalsIgnoreCase("")){
				Toast.makeText(mContext, "Name of group not accept blank value!", Toast.LENGTH_LONG).show();
			}else {
				addNewGroup(txtGroupName.getText().toString().trim());
			}
			break;
		case R.id.btnExit:
			dialog.dismiss();
			break;
		case R.id.btnColor1:
			mColorGroup = "Color1";
			txtGroupName.setBackgroundResource(R.color.Color1);
			break;
		case R.id.btnColor2:
			mColorGroup = "Color2";		
			txtGroupName.setBackgroundResource(R.color.Color2);
			break;
		case R.id.btnColor3:
			mColorGroup = "Color3";
			txtGroupName.setBackgroundResource(R.color.Color3);
			break;
		case R.id.btnColor4:
			mColorGroup = "Color4";
			txtGroupName.setBackgroundResource(R.color.Color4);
			break;
		case R.id.btnColor5:
			mColorGroup = "Color5";
			txtGroupName.setBackgroundResource(R.color.Color5);
			break;
		default:
			break;
		}
	}
	
	public void addNewGroup(String _groupName){
		int count = 0;
		mObjGroupDao = new GroupNoteDao(mContext);
		mObjGroupDao.openDB();
		mObjGroupModel = new GroupModel();
		mObjGroupModel.setGroupName(txtGroupName.getText().toString().trim());
		mObjGroupModel.setColor(mColorGroup);
		Cursor mCursor = mObjGroupDao.getGroupByName(mObjGroupModel);
		if (mCursor.moveToFirst()) {
			do {
				count = count +1;
			} while (mCursor.moveToNext());
		}
		if(count == 0){
			mObjGroupDao.insertGroup(mObjGroupModel);
			mObjGroupDao.closeDB();
			Intent callMainNote = new Intent(mContext, MainActivity.class);
			mContext.startActivity(callMainNote);
		}else{
			Toast.makeText(mContext, "This group is existed! Please input another group.", Toast.LENGTH_LONG).show();
			mObjGroupDao.closeDB();
		}
	}
	
	public void displaydialog() {
		dialog = new Dialog(mContext);
		dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
		WindowManager.LayoutParams lp = this.getWindow().getAttributes();
		lp.dimAmount = 0;
		dialog.setContentView(R.layout.layout_add_new_group);
		//dialog.getWindow().getAttributes().windowAnimations = R.style.Animations_SmileWindow;
		dialog.getWindow().getAttributes().windowAnimations = R.style.DialogAnimation;
		txtGroupName = (TextView) dialog.findViewById(R.id.txtGroupName);
		btnCancel = (Button) dialog.findViewById(R.id.btnExit);
		btnOk = (Button) dialog.findViewById(R.id.btnOk);
		btnColor1 = (Button) dialog.findViewById(R.id.btnColor1);
		btnColor2 = (Button) dialog.findViewById(R.id.btnColor2);
		btnColor3 = (Button) dialog.findViewById(R.id.btnColor3);
		btnColor4 = (Button) dialog.findViewById(R.id.btnColor4);
		btnColor5 = (Button) dialog.findViewById(R.id.btnColor5);
		mColorGroup = "Color1";
		txtGroupName.setBackgroundResource(R.color.Color1);
		
		btnOk.setOnClickListener(this);
		btnCancel.setOnClickListener(this);
		btnColor1.setOnClickListener(this);
		btnColor2.setOnClickListener(this);
		btnColor3.setOnClickListener(this);
		btnColor4.setOnClickListener(this);
		btnColor5.setOnClickListener(this);
		dialog.show();
	}
}
